<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
       <el-row :gutter="20">
          <el-col :span="4">
            <el-button type="primary" class="addClassify" @click="addCateClick">添加分类</el-button>
          </el-col>
        </el-row>
        <zk-table :data="goodsList" 
                  :columns="columns"
                  :selection-type="false"
                  :expand-type="false"
                  show-index
                  border>
          <template slot="isOk" slot-scope="scope">
            <i class="el-icon-success" v-if="scope.row.cat_deleted === true"></i>
            <i class="el-icon-error" v-else style="color: red"></i>
          </template>
          <template slot="sort" slot-scope="scope">
            <el-tag type="success" 
                    v-if="scope.row.cat_level === 0">
                    等级一
            </el-tag>
            <el-tag type="warning" 
                    v-else-if="scope.row.cat_level === 1">
                    等级二
            </el-tag>
            <el-tag type="danger" v-else>
              等级三
            </el-tag>
          </template>
          <template slot="action" slot-scope="scope">
             <el-button type="primary">编辑</el-button>
            <el-button type="danger">删除</el-button>
          </template>
        </zk-table>
        <!-- 分页操作 -->
        <!-- 每条页数改变时会触发 @size-change
         当前页数改变时会触发@current-change-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryList.pagenum"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="queryList.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
    </el-card>
    <!-- 添加分类按钮 dialog对话框 -->
    
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialog"
      width="50%"
      @close="addDialogClosed">
      <el-form :model="addCateInfo" :rules="addCateInfoRules" ref="addCateInfoRef" label-width="100px">
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateInfo.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader
            v-model="cascaderValue"
            :options="cascaderData"
            :props="cascaderProps"
            @change="cascaderDataChange"
            clearable>
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialog = false">取 消</el-button>
        <el-button type="primary" @click="confirmDialog">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryList: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      goodsList:[],
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "isOk"
        },
        {
          label: "排序",
          prop: "cat_level",
          type: "template",
          template: "sort"
        },
        {
          label: "操作",
          type: "template",
          template: "action"
        }

      ],
      // 添加分类打开关闭
      addCateDialog: false,
      // 添加分类名称
      addCateInfo: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      // 添加分类名称验证
      addCateInfoRules: {
        cat_name: [
            { required: true, message: '请输入分类名称', trigger: 'blur' },
            { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
          ],
      },
      cascaderData: [],
      cascaderValue: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover' ,
        checkStrictly: true
      }
    }
  },
  methods: {
    async getGoodsList() {
      const {data: res} = await this.$http.get('categories',{params: this.queryList});
      if(res.meta.status !== 200) {
        return this.$message.error("获取商品列表失败");
      }
      console.log(res.data);
      this.goodsList = res.data.result;
      this.total = res.data.total;
    },
    // 每条页数改变时会触发
    handleSizeChange(newSize) {
      this.queryList.pagesize = newSize;
      this.getGoodsList();
    },
    handleCurrentChange(pageNum) {
      this.queryList.pagenum = pageNum;
      this.getGoodsList();
    },
    // 获取父级分类列表
    async getParentList() {
      const {data: res} = await this.$http.get('categories',{params: {type: 2}})
      if(res.meta.status !== 200) return this.$message.error("获取父级分类列表失败")
      this.cascaderData = res.data;
      console.log(this.cascaderData);
    },
    // 打开添加分类dialog
     addCateClick() {
      this.getParentList();
      this.addCateDialog = true;
    },
    cascaderDataChange() {
      if(this.cascaderValue.length > 0) {
        this.addCateInfo.cat_pid = this.cascaderValue[this.cascaderValue.length - 1];
        this.addCateInfo.cat_level = this.cascaderValue.length;
      } else {
        this.addCateInfo.cat_pid = 0;
        this.addCateInfo.cat_level = 0;
      }
    },
    confirmDialog() {
      this.$refs.addCateInfoRef.validate(async valid => {
        if(!valid) return this.$message.error("验证不通过"); 
        const {data: res} = await this.$http.post('categories',this.addCateInfo)
        if(res.meta.status !== 201) return this.$message.error("添加分类失败")
        this.$message.success("添加分类成功");
        this.getGoodsList();
        this.addCateDialog = false;
      })
      
    },
    // 监听dialog关闭框
    addDialogClosed() {
      this.$refs.addCateInfoRef.resetFields();
      this.addCateInfo.cat_level  = 0;
      this.addCateInfo.cat_pid = 0;
      this.cascaderValue = [];
    }
  },
  created() {
    this.getGoodsList();
  }
}
</script>

<style lang="less" scoped>
.el-icon-success {
  color: lightgreen;
}
.addClassify {
  margin-bottom: 7px;
}
.el-cascader {
  width: 100%;
}
</style>